// menuSlice.ts
import { createSlice } from "@reduxjs/toolkit";

interface MenuState {
  selectedKey: string;
  openKeys: string[];
}

const initialState: MenuState = {
  selectedKey: "/home/index",
  openKeys: [], // 初始化为空数组
};

const menuSlice = createSlice({
  name: "menu",
  initialState,
  reducers: {
    setSelectedKey: (state, action) => {
      state.selectedKey = action.payload;
    },
    setOpenKeys: (state, action) => {
      state.openKeys = action.payload;
    },
    addOpenKey: (state, action) => {
      if (!state.openKeys.includes(action.payload)) {
        state.openKeys.push(action.payload);
      }
    },
  },
});

export const { setSelectedKey, setOpenKeys, addOpenKey } = menuSlice.actions;

export default menuSlice.reducer;
